/**
 * @description 录音控件
 */

export default {
	data() {
		return {
			rc: null
		}
	},
	methods: {
		// 创建控件
		createRecorderFn(params = {
			onStart: () => {},
			onStop: () => {}
		}) {
			// 验证权限
			// #ifdef MP
			uni.authorize({
				scope: 'scope.record',
				success: () => {
					this.createRecorderMainFn(params);
				},
				fail: () => {
					uni.showModal({
						content: '检测到您没打开录音功能权限，是否去设置打开？',
						confirmText: "确认",
						cancelText: '取消',
						success: res => {
							if (res.confirm) {
								uni.openSetting({
									success: res => {
										if (res.authSetting['scope.record']) {
											this.createRecorderMainFn(params);
										}
									}
								})
							} else {
								return false;
							}
						}
					})
				}
			})

			// #endif
		},
		// 核心
		createRecorderMainFn(params = {
			onStart: () => {},
			onStop: () => {}
		}) {
			// 录音
			this.rc = null;
			// #ifdef MP
			const recorderManager = uni.getRecorderManager();
			recorderManager.onStart(() => {
				console.log("开始录音")
				params.onStart && params.onStart()
			})
			recorderManager.onStop(res => {
				console.log("停止录音");
				params.onStop && params.onStop(res);
			});
			recorderManager.onError(res => {
				res.errMsg && uni.showModal({
					content: res.errMsg,
					showCancel: false
				})
			})
			this.rc = recorderManager;
			this.rc.start({format:"mp3", });
			// #endif
		}
	}
}